package com.by.zhdoctor.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.by.zhdoctor.domain.ZhengXiangMing;

import com.by.zhdoctor.repository.ZhengXiangMingRepository;
import com.by.zhdoctor.web.rest.util.HeaderUtil;
import com.by.zhdoctor.web.rest.util.PaginationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing ZhengXiangMing.
 */
@RestController
@RequestMapping("/api")
public class ZhengXiangMingResource {

    private final Logger log = LoggerFactory.getLogger(ZhengXiangMingResource.class);

    @Inject
    private ZhengXiangMingRepository zhengXiangMingRepository;

    /**
     * POST  /zheng-xiang-mings : Create a new zhengXiangMing.
     *
     * @param zhengXiangMing the zhengXiangMing to create
     * @return the ResponseEntity with status 201 (Created) and with body the new zhengXiangMing, or with status 400 (Bad Request) if the zhengXiangMing has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/zheng-xiang-mings")
    @Timed
    public ResponseEntity<ZhengXiangMing> createZhengXiangMing(@RequestBody ZhengXiangMing zhengXiangMing) throws URISyntaxException {
        log.debug("REST request to save ZhengXiangMing : {}", zhengXiangMing);
        if (zhengXiangMing.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("zhengXiangMing", "idexists", "A new zhengXiangMing cannot already have an ID")).body(null);
        }
        ZhengXiangMing result = zhengXiangMingRepository.save(zhengXiangMing);
        return ResponseEntity.created(new URI("/api/zheng-xiang-mings/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("zhengXiangMing", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /zheng-xiang-mings : Updates an existing zhengXiangMing.
     *
     * @param zhengXiangMing the zhengXiangMing to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated zhengXiangMing,
     * or with status 400 (Bad Request) if the zhengXiangMing is not valid,
     * or with status 500 (Internal Server Error) if the zhengXiangMing couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/zheng-xiang-mings")
    @Timed
    public ResponseEntity<ZhengXiangMing> updateZhengXiangMing(@RequestBody ZhengXiangMing zhengXiangMing) throws URISyntaxException {
        log.debug("REST request to update ZhengXiangMing : {}", zhengXiangMing);
        if (zhengXiangMing.getId() == null) {
            return createZhengXiangMing(zhengXiangMing);
        }
        ZhengXiangMing result = zhengXiangMingRepository.save(zhengXiangMing);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("zhengXiangMing", zhengXiangMing.getId().toString()))
            .body(result);
    }

    /**
     * GET  /zheng-xiang-mings : get all the zhengXiangMings.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of zhengXiangMings in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/zheng-xiang-mings")
    @Timed
    public ResponseEntity<List<ZhengXiangMing>> getAllZhengXiangMings(Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of ZhengXiangMings");
        Page<ZhengXiangMing> page = zhengXiangMingRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/zheng-xiang-mings");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /zheng-xiang-mings/:id : get the "id" zhengXiangMing.
     *
     * @param id the id of the zhengXiangMing to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the zhengXiangMing, or with status 404 (Not Found)
     */
    @GetMapping("/zheng-xiang-mings/{id}")
    @Timed
    public ResponseEntity<ZhengXiangMing> getZhengXiangMing(@PathVariable Long id) {
        log.debug("REST request to get ZhengXiangMing : {}", id);
        ZhengXiangMing zhengXiangMing = zhengXiangMingRepository.findOne(id);
        return Optional.ofNullable(zhengXiangMing)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /zheng-xiang-mings/:id : delete the "id" zhengXiangMing.
     *
     * @param id the id of the zhengXiangMing to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/zheng-xiang-mings/{id}")
    @Timed
    public ResponseEntity<Void> deleteZhengXiangMing(@PathVariable Long id) {
        log.debug("REST request to delete ZhengXiangMing : {}", id);
        zhengXiangMingRepository.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("zhengXiangMing", id.toString())).build();
    }


    /**
     * GET  /zheng-xings/:bmName : get the "id" bmName.
     *
     * @param bmName the bmName of the zhengXing to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the zhengXing, or with status 404 (Not Found)
     */
    @GetMapping("/zheng-xings-mings-byBM/{bmName}")
    @Timed
    public ResponseEntity<List<ZhengXiangMing>> getZhengXingByBMName(@PathVariable String bmName) {
        log.debug("REST request to get ZhengXing : {}", bmName);
        List<ZhengXiangMing> zhengXingMing = zhengXiangMingRepository.findAllByBMName(bmName);
        return Optional.ofNullable(zhengXingMing)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }



}
